#!/usr/bin/python
# -*- coding: cp1250 -*-

from pepe.glib import *

def char_generator():
    for a in string.letters[:26]:
        for b in string.letters[:26]:
            for c in string.letters[:26]:
                #for d in string.letters[:26]:
                yield string.lower("%s%s%s" % (a,b,c))

def broken_sentences(infl):
    of = open(infl + '.brs.txt', 'w')
    data = open(infl, 'r').read()

    data = re.sub(' {2,}', ' ', data)
    data = re.sub('[ \n]{2,}', '\n', data)
    data = re.sub('\n', '-@@-', data)

    #handling new lines placed inside sentence
    data = re.sub('\.\s{0,}-@@-', '.-@1-', data)
    data = re.sub('\"\s{0,}-@@-', '"-@1-', data)
    data = re.sub('\?\s{0,}-@@-', '?-@1-', data)
    data = re.sub('!\s{0,}-@@-', '!-@1-', data)
    data = re.sub('-@@-', ' ', data)
    data = re.sub('-@1-', '-@@-', data)
    
    data = re.sub('-@@-', '\n', data)
    of.write(data)

def abc_txt(infl, useid=False, charset='UTF-8'):
    ''' Text referencing utility

    documentation: abc.readme.abc
    '''
    iter = char_generator()
    html = '''<?xml version="1.0" encoding="%s" standalone="no"?>\n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">\n<html xmlns="http://www.w3.org/1999/xhtml">\n<meta http-equiv="Content-Type" content="text/html; charset=%s" />\n<head><title></title></head>\n<body>\n\n''' % (charset, charset)
    of = open(infl + '.ebk.html', 'w')
    of.write(html)
    data = open(infl, 'r').read()
    data = re.sub(' {2,}', ' ', data)
    data = re.sub('<', '&lt;', data)
    data = re.sub('>', '&gt;', data)
    data = re.sub('[ \n]{2,}', '\n', data)
    data = re.sub('\n', '-@@-', data)

    #test = open('test.out', 'w'); test.write(data); test.close()
    lines = string.split(data, '-@@-')
    for line in lines:
        skip_other = False
        if re.sub('\s{1,}', '', line) not in (''):
            row03 = re.sub('\s', '', line)[0:3]
            if   row03 == '!!!' and skip_other == False:
                skip_other = True; of.write("<h3>%s</h3><!-- %s -->\n" % (line[3:], iter.next()))
            elif row03 == '!!@' and skip_other == False:    
                skip_other = True; of.write("<h2>%s</h2><!-- %s -->\n" % (line[3:], iter.next()))
            elif row03 == '!@@' and skip_other == False:    
                skip_other = True; of.write("<h1>%s</h1><!-- %s -->\n" % (line[3:], iter.next()))
            else:
                if useid:
                    #openinkpot os
                    of.write("<p>&nbsp;&nbsp;%s -%s-</p>\n<p>&nbsp;</p>\n" % (line, iter.next()))
                    #hanlin os
                    #of.write("<p>&nbsp;%s -%s-</p>\n\n" % (line, iter.next()))
                else:
                    #openinkpot os
                    of.write("<p>&nbsp;%s</p>\n<p>&nbsp;</p>\n" % (line))
                    #hanlin os
                    #of.write("<p>&nbsp;%s</p>\n" % (line))
        else:
            of.write('\n')
    fhash = "<br>\n\tsource file sha1: &nbsp;%s\n" % (file_hash(infl, type='sha1'))
    modf = "20090824-0633 by vp"
    of.write('<sub>abc.py last modified &nbsp;' + modf + fhash + '</sub>')
    of.write('</body></html>\n')
    of.close()

if __name__ == "__main__":
    ''' '''
    #abc_txt(sys.argv[1])
    for f in glob.glob('*.abc'):
        #abc_txt(f, useid=True, charset='windows-1250')
        abc_txt(f, useid=True)
    for f in glob.glob('*.brs'):
        broken_sentences(f)
